using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.Presenters.Views;
using CMS.Presenters.Presenters;
using CMS.Web;
using CMS.Core.Domain;
using System.Collections.Generic;
using CMS.Data;

namespace CMS.Web.ListManager
{
    public partial class EditBaiViet : BaseUserControl, IEditBaiVietView
    {
        private EditBaiVietPresenter presenter;        
        private IList<BaiVietTheLoai> _TheLoaiList;
        bool isRole = false;
        NguoiSuDungInfo userInfo;
        private BaiViet obj;
        Int32 MaChucNang = 0;

        public EditBaiViet()
        {
            this.presenter = new EditBaiVietPresenter(this, NHibernateDaoFactory.Instance.GetBaiVietDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtImage.Attributes.Add("onkeypress", "return false;");
            txtImage.Attributes.Add("onkeyup", "return false;");
            txtImage.Attributes.Add("onpaste", "return false;");
            txtNgayDang.Attributes.Add("readonly", "true");
            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            MaChucNang = this.FName;
            this.presenter.LoadTheLoais(MaChucNang);
            presenter.InitViewForEditing(Convert.ToInt32(ProjectBase.Utils.CRequest.GetQueryUri("MaBV", "")), IsPostBack);
            this.obj = this.presenter.getByID(Convert.ToInt32(ProjectBase.Utils.CRequest.GetQueryUri("MaBV", "")));

            this.drpTheLoai.DataSource = this.TheLoaiList;
            this.drpTheLoai.DataBind();
            btnOK.Attributes.Add("onclick", "return validator();");
        }

        public String getFileDinhKem()
        {
            return obj.AnhDaiDien;
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            presenter.UpdateObj(Page.IsValid);             
            string strFile = CMS.Web.Files.Instance.UpLoadFile(this.txtImage.PostedFile, "");
            this.btnCancel_Click(this, null);
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=EDIT&MaChucNang="+this.MaChucNang+"&MaBV="+this.hidID.Value);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW");
        }


        #region IEditBaiVietView Members

        public string Message
        {
            set
            {
                //lblMessage.Text = value;  
            }
        }

        public BaiViet ObjToUpdate
        {            
            get
            {
                String strAnh = "";
                if (this.obj != null)
                {
                    obj.TieuDe = this.txtTieuDe.Text;
                    strAnh = CMS.Web.Files.Instance.StandardName("", this.txtImage.PostedFile);
                    if (!hidAnhDaiDien.Value.Equals(""))
                    {
                        if (strAnh.Equals(""))
                        {
                            strAnh = hidAnhDaiDien.Value;

                        }
                    }
                    //strAnh = CMS.Web.Files.Instance.StandardName("", this.txtImage.PostedFile);
                    obj.AnhDaiDien = strAnh;
                    obj.ChuThichAnh = this.txtImgDescr.Text;
                    obj.TomTat = this.txtSum.Text;
                    if (!txtNgayDang.Text.Trim().Equals(""))
                    {
                        string[] temp = txtNgayDang.Text.Trim().Split(new Char[] { '/' });
                        obj.NgayCapNhat = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                    obj.TrangThai = this.chkState.Checked;
                    obj.NoiDung = this.txtBody.Value;
                    obj.NguonTin = this.txtAuthor.Text;
                    obj.MaTheLoaiLookup = new BaiVietTheLoaiDao().GetById(Convert.ToInt32(this.drpTheLoai.SelectedValue));
                    obj.NguoiCapNhatLookup = new NguoiSuDungDao().GetById(userInfo.ID);
                }

                return obj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjToUpdate may not be null");

                this.hidID.Value = value.ID.ToString();
                this.hidAnhDaiDien.Value = value.AnhDaiDien;
                this.txtTieuDe.Text = value.TieuDe;
                this.drpTheLoai.SelectedValue = value.MaTheLoaiLookup.ID.ToString();
                this.txtImgDescr.Text = value.ChuThichAnh;
                this.txtSum.Text = value.TomTat;
                string[] temp = value.NgayCapNhat.ToString().Split(new Char[] { '/' });
                this.txtNgayDang.Text = temp[1] + "/" + temp[0] + "/" + temp[2].Substring(0, 4);
                this.txtAuthor.Text = value.NguonTin;
                this.txtBody.Value = value.NoiDung;
                this.chkState.Checked = value.TrangThai;
            }
        }

        public IList<BaiVietTheLoai> TheLoaiList
        {
            get
            {
                return this._TheLoaiList;
            }
            set
            {
                this._TheLoaiList = value;
            }
        }

        #endregion        
    }
}